package com.micodroid.glucosa;

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import com.jjoe64.graphview.GraphView.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.micodroid.baseclass.Medicion;
import com.micodroid.database.DataClass;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;

public class GraficaActivity extends Activity{
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			DataClass.initInstance(this);
	        	        
	        LineGraphView graphView = new LineGraphView(this, "Medición de Glucosa")
	        {  
	        	
	        	DateFormatSymbols dfs = new DateFormatSymbols();
	    		String[] meses = dfs.getMonths();
	    		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC-6"));
	        	
	        	@Override  
	        	   protected String formatLabel(double value, boolean isValueX) {  
	        	      if (isValueX) {  
	        	    	 if((int) value < DataClass.getInstance().getMediciones().size()-1) { 
	        	    		 Medicion medicion = DataClass.getInstance().getMediciones().get((int) value) ;
	        	    		 cal.setTimeInMillis(Long.parseLong(medicion.getFecha()));
	        	    		 return cal.get(Calendar.DAY_OF_MONTH) + " " + meses[cal.get(Calendar.MONTH)].substring(0, 3) +  " " + cal.get(Calendar.YEAR);
	        	    	 }
	        	    	 else {
	        	    		 return "...";
	        	    	 }
	        	    	  
	        	    
	        	    	
	        	    	 
	        	      } else return super.formatLabel(value, isValueX); 
	        	   }  
	        	};  
	        
	    
	      
	        List<GraphViewData> graphViewData = new ArrayList<GraphViewData>();
	        graphViewData = filldata();
	        GraphViewData[] graphViewData2 = new GraphViewData[graphViewData.size()];
	        graphViewData.toArray(graphViewData2);
	        
			graphView.addSeries(new GraphViewSeries(graphViewData2));
			graphView.setViewPort(2, 1);  
			graphView.setScrollable(true);  
			graphView.setScalable(true);  
	
			setContentView(graphView);
	    }
	 
	 private List<GraphViewData> filldata() {
		  List<GraphViewData> graphViewData = new ArrayList<GraphViewData>();
	      
		  for(int i = 0; i< DataClass.getInstance().getMediciones().size(); i++) {	
			  Medicion medicion = DataClass.getInstance().getMediciones().get(i);
			  graphViewData.add(new GraphViewData(i, medicion.getGlucosa()));
		  }
		
		  return graphViewData;
	 }
	 
	 /***
	 * Volvemos a cargar la instancia en caso de que sea necesario
	 */
	    
	    @Override
	    protected void onResume() {
	    	super.onResume();   
	    	DataClass.initInstance(this);
	    }
	 
}
